﻿using JNPF.Basics.Logic;
using JNPF.Basics.Models;
using JNPF.Basics.Models.Portal;
using JNPF.Utils;
using JNPF.Utils.Controller;
using Microsoft.AspNetCore.Mvc;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace JNPF.Basics.Controller
{
    /// <summary>
    /// 门户设计模块
    /// 版 本：V3.0.0
    /// 版 权：引迈信息技术有限公司
    /// 作 者：管理员/admin
    /// 日 期：2020-11-02 11:49
    /// </summary>
    [Route("api/[controller]")]
    public class PortalController : BaseApiController
    {
        private PortalBll portalBll = new PortalBll();
        private UserBll userBll = new UserBll();

        #region GET
        /// <summary>
        /// 列表
        /// </summary>
        /// <param name="input">请求参数</param>
        /// <returns>返回列表</returns>
        [HttpGet]
        public async Task<ResponseResult> GetList(PageInput input)
        {
            var userList = (await userBll.GetList()).FindAll(t => t.EnabledMark.Equals(1));
            var data = await portalBll.GetList(input);
            var list = new ArrayList();
            foreach (var item in data.list)
            {
                var creatorUser = userList.Where(u => u.Id == item.CreatorUserId).FirstOrDefault();
                var dicts = item.ToObject<Dictionary<string, object>>();
                dicts.Add("CreatorUser", creatorUser.RealName + "/" + creatorUser.Account);
                var lastModifyUser = userList.Where(u => u.Id == item.LastModifyUserId && item.LastModifyUserId != null).FirstOrDefault();
                if (lastModifyUser != null)
                {
                    dicts.Add("LastModifyUser", lastModifyUser.RealName + "/" + lastModifyUser.Account);
                }
                else
                {
                    dicts.Add("LastModifyUser", "");
                }
                list.Add(dicts.ToObject<PortalListOutput>());
            }
            var output = new PageResult()
            {
                list = list,
                pagination = data.pagination
            };
            return Success(output);
        }

        /// <summary>
        /// 获取门户侧边框列表
        /// </summary>
        /// <returns></returns>
        [HttpGet("Selector")]
        public async Task<ResponseResult> GetSelector(string type)
        {
            var data = await portalBll.GetSelector(type);
            var treeList = data.ToTree();
            return Success(new { list = treeList });
        }

        /// <summary>
        /// 获取门户信息
        /// </summary>
        /// <param name="id">主键id</param>
        /// <returns></returns>
        [HttpGet("{id}")]
        public async Task<ResponseResult> GetInfo(string id)
        {
            var data = await portalBll.GetInfo(id);
            var output = data.MapTo<PortalInfoOutput>();
            return Success(output);
        }

        /// <summary>
        /// 信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("{id}/auth")]
        public async Task<ResponseResult> GetInfoAuth(string id)
        {
            var auth = await portalBll.GetInfoAuth(id);
            if (auth != null)
            {
                return Success(new { formData = auth.FormData });
            }
            return Success("");
        }
        #endregion

        #region POST
        /// <summary>
        /// 新建功能信息
        /// </summary>
        /// <param name="input">实体对象</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ResponseResult> Create([FromBody] PortalCrInput input)
        {
            var entity = input.MapTo<PortalEntity>();
            await portalBll.Create(entity);
            return Success("新建成功");
        }

        /// <summary>
        /// 修改接口
        /// </summary>
        /// <param name="id">主键id</param>
        /// <param name="input">参数</param>
        /// <returns></returns>
        [HttpPut("{id}")]
        public async Task<ResponseResult> Update(string id, [FromBody] PortalCrInput input)
        {
            var entity = input.MapTo<PortalEntity>();
            var isOk = await portalBll.Update(id, entity);
            return isOk > 0 ? Success("修改成功") : Error("修改失败");
        }

        /// <summary>
        /// 删除接口
        /// </summary>
        /// <param name="id">主键id</param>
        /// <returns></returns>
        [HttpDelete("{id}")]
        public async Task<ResponseResult> Delete(string id)
        {
            var entity = await portalBll.GetInfo(id);
            var isOk = await portalBll.Delete(entity);
            return isOk > 0 ? Success("删除成功") : Error("删除失败");
        }

        /// <summary>
        /// 复制
        /// </summary>
        /// <param name="id">主键值</param>
        /// <returns></returns>
        [HttpPost("{id}/Actions/Copy")]
        public async Task<ResponseResult> ActionsCopy(string id)
        {
            var entity = await portalBll.GetInfo(id);
            entity.FullName = entity.FullName + "_副本";
            entity.EnCode = entity.EnCode + "_1";
            entity.EnabledMark = 0;
            entity.LastModifyTime = null;
            entity.LastModifyUserId = null;
            var isOk = await portalBll.Create(entity);
            return isOk > 0 ? Success("复制成功") : Error("复制失败");
        }

        /// <summary>
        /// 设置默认门户
        /// </summary>
        /// <returns></returns>
        [HttpPut("{id}/Actions/SetDefault")]
        public async Task<ResponseResult> SetDefault(string id)
        {
            UserEntity userEntity = await userBll.GetInfo(UserProvider.Instance.Get().UserId);
            if (userEntity != null)
            {
                userEntity.PortalId = id;
                var isOk = await userBll.UpdateState(userEntity.Id, userEntity);
                return isOk > 0 ? Success("设置成功") : Error("设置失败");
            }
            return Error("设置失败，用户不存在");
        }
        #endregion
    }
}
